var usernameAgree = false;
var phoneNoAgree = false;
var pwdAgree = false;
var repwdAgree = false;
var emailAgree = false;


function userCheck(value) {
    var userSpan = document.getElementById("userSpan");
    var username = value.value;
    var patt = /[\u4e00-\u9fa5a-zA-Z0-9]+$/
    console.log(patt.test(username))
    if (username == "") {
        userSpan.innerText = "用户名不能为空"
    } else if (username.length > 20) {
        userSpan.innerText = "用户名过长"
    } else if (!patt.test(username)) {
        userSpan.innerText = "用户名格式不正确"
    } else {
        userSpan.innerHTML = ""
        usernameAgree = true;
    }
}

function phoneNumberCheck(params) {
    var phoneNumberSpan = document.getElementById("phoneNumberSpan");
    var phoneNo = params.value;
    var patt = /\d{11}/
    if (phoneNo == "") {
        phoneNumberSpan.innerText = "必须输入手机号"
    } else if (!patt.test(phoneNo)) {
        phoneNumberSpan.innerText = "手机号格式不正确"
    } else {
        phoneNumberSpan.innerHTML = ""
        phoneNoAgree = true;
    }
}

function pwdCheck(params) {

    //密码正确性判断
    var pwdSpan = document.getElementById("pwdSpan")
    var pwd = params.value
    if (pwd == "") {
        pwdSpan.innerText = "请输入密码"
    } else if (/ +/.test(pwd)) {
        pwdSpan.innerText = "密码不能有空格"
    } else if (pwd.length > 20 || pwd.length < 6) {
        pwdSpan.innerText = "密码长度有误"
    } else {
        pwdSpan.innerText = "";
        pwdAgree = true
    }
}

function pwdStrCheck() {
    //密码强度判断
    var L = document.getElementById("str_L")
    var M = document.getElementById("str_M")
    var B = document.getElementById("str_B")
    var pwd = document.getElementById("pwd")
    L.style["backgroundColor"] = "#C0191F"
    var patt2 = /^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{1,}$/
    if (pwd.value.length < 6) {
        L.style["backgroundColor"] = "#C0191F"
        M.style["backgroundColor"] = "#d4d4d4"
        B.style["backgroundColor"] = "#d4d4d4"
    } else {
        if (/(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{1,}/.test(pwd.value)) {
            L.style["backgroundColor"] = "#53B300"
            M.style["backgroundColor"] = "#53B300"
            B.style["backgroundColor"] = "#53B300"
        } else {
            if (patt2.test(pwd.value)) {
                console.log(2)
                L.style["backgroundColor"] = "#FFDA00"
                M.style["backgroundColor"] = "#FFDA00"
                B.style["backgroundColor"] = "#d4d4d4"
            } else {
                L.style["backgroundColor"] = "#C0191F"
                M.style["backgroundColor"] = "#d4d4d4"
                B.style["backgroundColor"] = "#d4d4d4"
            }
        }
    }
    if (pwd.value == "") {
        L.style["backgroundColor"] = "#d4d4d4"
        M.style["backgroundColor"] = "#d4d4d4"
        B.style["backgroundColor"] = "#d4d4d4"
    }
}

function repwdCheck(params) {
    var repwd = params.value;
    var repwdSpan = document.getElementById("repwdSpan");
    var pwd = document.getElementById("pwd").value;
    console.log(pwd)
    console.log(repwd)
    if (repwd == "") {
        repwdSpan.innerText = "请确认密码"
    } else if (repwd != pwd) {
        repwdSpan.innerText = "两次密码输入不一致"
    } else {
        repwdSpan.innerText = ""
        repwdAgree = true
    }
}

function emailCheck(params) {
    var email = params.value;
    var emailSpan = document.getElementById("emailSpan");
    var patt = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
    if (email == "") {
        emailSpan.innerText = "邮箱不能为空"
    } else if (!patt.test(email)) {
        emailSpan.innerText = "邮箱格式有误"
    } else {
        emailSpan.innerText = ""
        emailAgree = true;
    }
}

function agreeCheck(params) {
    var agree = params.checked;
    var loginBtn = document.getElementById("loginBtn")
    console.log(agree)
    console.log(loginBtn)
    if (agree) {
        loginBtn.className = "registerbutton"
        loginBtn.disabled = false
    } else {
        loginBtn.className = "registerbuttonfalse"
        loginBtn.disabled = true
    }
}

function loginCheck() {
    userCheck(document.getElementById("user"))
    phoneNumberCheck(document.getElementById("phoneNo"))
    pwdCheck(document.getElementById("pwd"))
    repwdCheck(document.getElementById("repwd"))
    emailCheck(document.getElementById("Email"))
    if (usernameAgree && pwdAgree && repwdAgree && phoneNoAgree && emailAgree) {
        window.location.href = "../html/login.html"
    }
}